'use strict';

  angular.module('app')
    .controller('funcController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
    								function ($scope, $rootScope, $state, $timeout, toaster) {
    	
    	var title = "功能分配";
    	$scope.title = $rootScope.title = title;
		var id = $state.params.id;
    	activate(id);
		$scope.toggle = function (scope) {
            scope.toggle();
          };

          $scope.collapseAll = function () {
            $scope.$broadcast('angular-ui-tree:collapse-all');
          };
          $scope.expandAll = function () {
            $scope.$broadcast('angular-ui-tree:expand-all');
          };
          $scope.submit= function(){
        	  var menuIds=new Array();
              menuIds=$("input[name='menuId']:checked").val([]);//此为重点
              var str = "";
              for(var j=0;j<menuIds.length;j++){
            	  str=str+menuIds[j].value+','; 
              }
              $scope.isDisabled = true;//提交disabled
              $.ajax({
  				url : $scope.app.project_name+'/role/update/updateSysRoleMenuFunc',
  				data: {'arryFunc': str,'roleId': id},
  				traditional: true
  			  }).then(callback);
              function callback(result) {
                  if(result.httpCode ==200){//成功
                      toaster.clear('*');
                      toaster.pop('success', '', "保存成功");
                      $timeout(function(){
                          $state.go('main.sys.role.list');
                      },2000);
                  }else{
                      toaster.clear('*');
                      toaster.pop('error', '', result.msg);
                      $scope.isDisabled = false;
                  }
              }
          }
          
       // 初始化页面
          function activate(id) {
  	        $scope.loading = true;
          	$.ajax({
  				url : $scope.app.project_name+'/role/read/getMenuFuncList',
  				data: {'id': id}
  			}).then(function(result) {
  		        $scope.loading = false;
  				if (result.httpCode == 200) {
  					$scope.data = result.data;
  					console.log($scope.data );
  				} else {
  					$scope.msg = result.msg;
  				}
  				$scope.$apply();
  			});
          }
          
    }]);
